#################################################2.服务器端用mysql存储购物车信息######################################################

from rest_framework.views import APIView
from .serializers import CartsSerializer
from rest_framework.response import Response

class CartsView(APIView):

    def post(self,request):

        try:
            user = request.user
        except Exception:
            user = None

        if not user:
            #用户未登录，存储到cookie中
            serializer = CartsSerializer(data=request.data)
            serializer.is_valid(raise_exception=True)
            pass
        else:
            #用户已登录，存储到mysql中
            serializer = CartsSerializer(data=request.data,context={'request':request,'view':self})
            serializer.is_valid(raise_exception=True)
            serializer.save()
            return Response(serializer.data)



